package com.tech.arno.spicy.ui.fragment.home;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tech.arno.spicy.R;
import com.tech.arno.spicy.module.recommand.BaseRecommandModel;
import com.tech.arno.spicy.module.recommand.RecommandValue;
import com.tech.arno.spicy.mvp.presenter.NewsPresenter;
import com.tech.arno.spicy.mvp.presenterImpl.NewsPresenterImpl;
import com.tech.arno.spicy.mvp.view.NewsView;
import com.tech.arno.spicy.share.ShareDialog;
import com.tech.arno.spicy.staticVal.Constant;
import com.tech.arno.spicy.ui.activity.MyPhotoViewActivity;
import com.tech.arno.spicy.ui.activity.MySearchActivity;
import com.tech.arno.spicy.ui.adapter.CourseAdapter;
import com.tech.arno.spicy.ui.fragment.BaseFragment;
import com.tech.arno.spicy.ui.view.HomeHeaderView;
import com.tech.arno.spicy.zxing.app.CaptureActivity;
import com.tech.arno.spicysdk.videosdk.activity.AdBrowserActivity;

import cn.sharesdk.framework.Platform;

/*
*   @package:     com.tech.arno.spicy.ui.fragment.home
*   @fileName:    NewsFragment
*   @author:    ArnoFrost
*   @date:  2017/12/13 0013 11:03
*   @desc:     首页fragment
*/

public class NewsFragment extends BaseFragment implements NewsView, View.OnClickListener, AdapterView.OnItemClickListener {
    private static final String TAG = "NewsFragment";
    private static final int REQUEST_QRCODE = 0x01;
    /*
    * UI
    * */
    private View mContentView;
    private ListView mListView;//数据展示ListView
    private TextView mCategoryView;//分类
    private TextView mSearchView;//搜索
    private ImageView mLoadingView;//加载ImageView
    private TextView mQrecodeView;//二维码

    private NewsPresenter presenter;

    /*
    * data
    * */

    private CourseAdapter mAdapter;
    private BaseRecommandModel mRecommandDada;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: " + "NewsFragment");

//        requestRecommandData();
        presenter = new NewsPresenterImpl(this);
        presenter.requestData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mContentView = inflater.inflate(R.layout.fragment_news_layout, container, false);
        initView(mContentView);
        return mContentView;
    }

    @Override
    public void onDestroyView() {
        presenter.onDestroy();
        super.onDestroyView();
    }

    public NewsFragment() {
    }


    private void initView(View mContentView) {
        mQrecodeView = (TextView) mContentView.findViewById(R.id.qrcode_view);
        mQrecodeView.setOnClickListener(this);
        mCategoryView = (TextView) mContentView.findViewById(R.id.category_view);
        mCategoryView.setOnClickListener(this);
        mSearchView = (TextView) mContentView.findViewById(R.id.search_view);
        mSearchView.setOnClickListener(this);
        mListView = (ListView) mContentView.findViewById(R.id.list_view);
        mListView.setOnItemClickListener(this);
        mLoadingView = (ImageView) mContentView.findViewById(R.id.loading_view);
        //启动LoadingView动画
        AnimationDrawable anim = (AnimationDrawable) mLoadingView.getDrawable();
        anim.start();


    }
    @Override
    public void loadFail(Object reasonObj) {
        Log.d(TAG, "onFailure: " + reasonObj.toString());
        showErrorView();
    }

    @Override
    public void loadSuccess(Object responseObj) {
        Log.d(TAG, "onSuccess: " + responseObj.toString());
        mRecommandDada = (BaseRecommandModel) responseObj;
        showSuccessView();
    }

    /**
     * @function 请求成功执行方法
     * @author ArnoFrost
     * @time 2017/12/8 0008  9:39
     */
    @Override
    public void showSuccessView() {
        //判断是否为空保障健壮性
        if (mRecommandDada.data.list != null && mRecommandDada.data.list.size() > 0) {
            mLoadingView.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);

            //为ListView添加列表头
            mListView.addHeaderView(new HomeHeaderView(mContext, mRecommandDada.data.head));
            //创建
            mAdapter = new CourseAdapter(mContext, mRecommandDada.data.list);
            mListView.setAdapter(mAdapter);
            mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {

                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    mAdapter.updateAdInScrollView();
                }
            });
        } else {
            showErrorView();

        }
    }

    /**
     * @function 请求失败执行方法
     * @author ArnoFrost
     * @time 2017/12/8 0008  9:40
     */
    @Override
    public void showErrorView() {
    }

    @Override
    public void scanQrcode() {
        //判断是否有权限
        if (hasPermission(Constant.HARDWEAR_CAMERA_PERMISSION) && hasPermission(Constant.READ_PHONE_STATE_PERMISSON)) {
            //拥有权限后执行
            doOpenCamera();
        } else {
            //没有权限申请
            requestPermission(Constant.HARDWEAR_CAMERA_CODE, Constant.HARDWEAR_CAMERA_PERMISSION);
            requestPermission(Constant.READ_PHONE_CODE, Constant.READ_PHONE_STATE_PERMISSON);
        }
    }

    @Override
    public void category() {
        ShareDialog dialog = new ShareDialog(mContext, false);
        dialog.setShareType(Platform.SHARE_IMAGE);
        dialog.setShareTitle("周杰伦");
        dialog.setShareText("我很忙");
        dialog.setImagePhoto(Environment.getExternalStorageDirectory() + "/test2.jpg");
        dialog.show();
    }

    @Override
    public void goSearch() {
        Intent searchIntent = new Intent(mContext, MySearchActivity.class);
        mContext.startActivity(searchIntent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qrcode_view:
                scanQrcode();
                break;
            case R.id.category_view:
                category();
                break;
            case R.id.search_view:
                goSearch();
                break;
        }
    }

    /**
     * @name onItemClick
     * @function 列表跳转大图加载
     * @author ArnoFrost
     * @time 2017/12/18 0018  10:19
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //注意减去列表头部
        RecommandValue value = (RecommandValue) mAdapter.getItem(
                position - mListView.getHeaderViewsCount());
        if (value.type != 0) {
            //将数据传送到大图加载Activity
            goPhoto(value);
        }
    }

    @Override
    public void goPhoto(RecommandValue value) {
        Intent intent = new Intent(mContext, MyPhotoViewActivity.class);
        intent.putStringArrayListExtra(MyPhotoViewActivity.PHOTO_LIST, value.url);
        startActivity(intent);
    }

    /**
     * @name doOpenCamera
     * @function 申请通过后回调
     * @author ArnoFrost
     * @time 2017/12/19 0019  15:05
     */
    @Override
    public void doOpenCamera() {
        Intent intent = new Intent(mContext, CaptureActivity.class);
        startActivityForResult(intent, REQUEST_QRCODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_QRCODE:
                if (resultCode == Activity.RESULT_OK) {
                    //获取数据
                    String code = data.getStringExtra("SCAN_RESULT");
                    if (code.contains("http") || code.contains("https")) {
                        Intent intent = new Intent(mContext, AdBrowserActivity.class);
                        intent.putExtra(AdBrowserActivity.KEY_URL, code);
                        startActivity(intent);
                    } else {
                        Toast.makeText(mContext, code, Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }


}
